package com.youlu.campus.service.activity.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.entity.ActivityCmsSlideshow;
import com.youlu.campus.entity.VO.req.ActivityCmsSlideshowQueryVO;
import com.youlu.campus.service.activity.ActivityCmsSlideshowService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ActivityCmsSlideshowImpl implements ActivityCmsSlideshowService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page<ActivityCmsSlideshow> list(ActivityCmsSlideshowQueryVO req) {
        log.info(":>>> 开始获取活动轮播图配置:{}", JSON.toJSONString(req));
        if (Objects.isNull(req)) {
            log.error(":>>> 获取广告信息错误:参数为空");
            req = new ActivityCmsSlideshowQueryVO();
            req.setPage(1);
            req.setPageSize(10);
        }
        Query query = new Query();
        if (StringUtils.isNotBlank(req.getActivityId())) {
            query.addCriteria(Criteria.where("activityId").is(req.getActivityId()));
        }
        Long count = mongoTemplate.count(query, ActivityCmsSlideshowQueryVO.class);
        PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<ActivityCmsSlideshow> infos = mongoTemplate.find(query.with(pageRequest), ActivityCmsSlideshow.class);
        return PageableExecutionUtils.getPage(infos, pageRequest, () -> count);
    }

    @Override
    public ActivityCmsSlideshow find(String id) {
        Query query = new Query().addCriteria(Criteria.where("id").is(id));
        ActivityCmsSlideshow d = mongoTemplate.findOne(query, ActivityCmsSlideshow.class);
        return d;
    }

    @Override
    public boolean create(@NotNull ActivityCmsSlideshow req) {
        req.setId(null);
        req.setCreatedTime(new Date());
        mongoTemplate.insert(req);
        return true;
    }

    @Override
    public boolean update(@NotNull ActivityCmsSlideshow req) {
        Query query = new Query().addCriteria(Criteria.where("id").is(req.getId()));
        Update update = new Update();
        boolean updated = false;
        if (StringUtils.isNotBlank(req.getDisplay())) {
            update.set("display", req.getDisplay());
            updated = true;
        }
        if (!CollectionUtils.isEmpty(req.getImageUrl())) {
            update.set("imageUrl", req.getImageUrl());
            updated = true;
        }
        if (StringUtils.isNotBlank(req.getLocation())) {
            update.set("location", req.getLocation());
            updated = true;
        }
        if (updated) {
            update.set("updatedTime", new Date());
            UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ActivityCmsSlideshow.class);
            return updateResult.getModifiedCount() > 0 ? true : false;
        }
        return false;
    }

    @Override
    public boolean delete(String id) {
        Query query = new Query().addCriteria(Criteria.where("id").is(id));
        DeleteResult deleteResult = mongoTemplate.remove(query, ActivityCmsSlideshow.class);
        return deleteResult.getDeletedCount() > 0 ? true : false;
    }

    @Override
    public List<ActivityCmsSlideshow> findByActivityId(String activityId) {
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId));
        List<ActivityCmsSlideshow> list = mongoTemplate.find(query, ActivityCmsSlideshow.class);
        return list;
    }
}
